package com.ease.dao.statement.factory;

/**
 *
 * @author nathanleewei
 *
 */
import java.util.ArrayList;
import java.util.List;

import com.ease.common.util.Detect;

public class NamedStatementMultiFactory extends Detect implements NamedStatementFactory {

	private List<NamedStatementFactory> factorys;

	public String getNamedStatement(String namedStatementName, Object parameter) {
		if (null != factorys) {
			NamedStatementFactory factory = null;
			String namedStatement = null;
			for (int i = 0; i < factorys.size(); i++) {
				factory = (NamedStatementFactory) factorys.get(i);
				namedStatement = factory.getNamedStatement(namedStatementName, parameter);
				if (notEmpty(namedStatement)) {
					return namedStatement;
				}
			}
			factory = null;
		}
		return null;
	}

	/**
	 * @return Returns the factorys.
	 */
	public NamedStatementMultiFactory addFactory(NamedStatementFactory factory) {
		if (null != factory) {

			if (null == factorys) {
				factorys = new ArrayList<NamedStatementFactory>();
			}

			factorys.add(factory);
		}
		return this;
	}

	/**
	 * @return Returns the factorys.
	 */
	public List<NamedStatementFactory> getFactorys() {
		return factorys;
	}

	/**
	 * @param factorys
	 *            The factorys to set.
	 */
	public void setFactorys(List<NamedStatementFactory> factorys) {
		this.factorys = factorys;
	}

}
